Operators and Static

Objectives


Discussion

Math Operators

Operation Operator Examples
Add + newAge = oldAge + 1
age = age + 1
Subtract - numEggs = 12 - numBroken
x = y - z
Multiply * c = 2*3.14*r
Divide / speed = distance/time
Exponent ^ area = 3.14*(r^2)
Modulus Mod remainder = number mod 2
Integer Div \ numInto = 12 \ 5

Operator Precedence

x = 3 + 5 * 6 - 2

x = (3 + 5) * (6 - 2)

Concatenation Operator for Strings

Controlling Variable Life with Static

Back to top


Demonstration

1.  Open your project for Unit 2.  Add another form.  Give the form a name such as "Operators".  Make this new form the startup form.

2.  Add two textboxes (txt1 and txt2) and a button (btnAdd).  Add the following code to the click event for the button:

Dim i, j, k As Single
i = txt1.Text
j = txt2.Text
k = i + j
MessageBox.Show("The answer is: " & k)

3. Run the program.  Type numbers in the textboxes (whole or real numbers) and click the button.  The messagebox should display the sum of the two numbers.  Notice that the messagebox displays a string.  The string that it is displaying in this situation is made by adding the string "The answer is: " and the value of the variable k.  Study this code carefully.  VB is able to convert the number that is stored in k into a string and add that string to the string "The answer is: ". 

4.  Now run the program but put text into the textboxes and click the button.  You should get an error because i and j are expecting numbers but VB can NOT convert the strings that you entered into numbers.  We will learn how to solve this problem in a future lesson using the function "Isnumeric()".

5.  Now change your code so that i, j, and k are declared as strings (Dim i, j, k As Strings).  Run the program and type letters or words in the textboxes.  Check out the answer.  Now type numbers in the textboxes and click the button.  Notice that i and j have been added as strings;  not surprising since they are declared as strings.

6.  Now we will experiment with Static.  Add another button (btnCount).  We will now try to add code that will keep track of the number of times that you click the button and display the value of the variable in a messagebox.  We will first declare the variable with Dim just to see what happens. Add the following code to the click event:

Dim iCount As Byte
iCount = iCount + 1
Dim sMessage As String = "The value of iCount is now "
sMessage = sMessage & iCount
MessageBox.Show(sMessage)

7.  Run the program and click the button a few times.  Notice that the value keeps showing as 1.  Everytime the subroutine for the event executes, the variable iCount is declared and set equal to zero.  Then 1 is added to iCount to make it 1.  The value of iCount (1) is added to sMessage and sMessage is displayed in the messagebox.  As soon as the subroutine ends, the variable iCount is deleted or freed from memory so its value is gone.  Now when you click the button again, the cycle is repeated and iCount is created again, incremented and so on.  If we want to keep the value of iCount around between clicks we need to use Static.

8.  Change the word "Dim" to "Static" in your code.  Run the program and click the button a few times.  Notice that it is keeping track of the number of times that you clicked the button.  The Static keyword ensures that the VB "remembers" the value of iCount.

 Back to top


Exercises

1.  In each situation, what is the value of x? You should try to figure these out in your head first and then verify in code.

a.  x = 5 + 10 / 2 
b.  x = (5 + 10)/3
c.  x = 3.14*3^2.0
d.  x = 100 + 50 * 4 - 1
e.  x = (100 + 50) * (4 -1)
f. x = 100 mod 30
g. x = 100 \ 30
h. x = "hi" & " " & "bob"
i. x = "hi " + "bob"

2.  Write a program that has two textboxes (txtNum1 and txtNum2) and three buttons (btnDivide, btnExponent, and btnModulus). 

3. Write a program that has a two buttons (btnAdd and btnDisplayTotal), a textbox, and a label

Back to top

Links & Help
Back to top